package com.joseph.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import com.joseph.annotation.RequireRole;

/**
 * @description 
 * @author Joseph_Mok
 * @date 2016年3月29日下午2:53:11
 */
public class RoleProxyCglib implements MethodInterceptor {

	private Object target;  
	
	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy arg3) throws Throwable {
		Annotation annotation = method.getAnnotation(RequireRole.class);
		if (annotation != null) {
			System.out.println("need role");
			return null;
		}
		method.invoke(target, args);
		return null;
	}
	
	public Object getInstance(Object target) {  
        this.target = target;  
        Enhancer enhancer = new Enhancer();  
        enhancer.setSuperclass(this.target.getClass());  
        // 回调方法  
        enhancer.setCallback(this);  
        // 创建代理对象  
        return enhancer.create();  
	}  
}
